// pages/my/my/my.js
const app = getApp();
const requestUtil = require("../../../utils/request");
const api = require("../../../config/api");
const util = require("../../../utils/util");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false, // 判断是否已登录
    userType: null, // 用户角色，1教师，2家长，null未注册
    name: null, // 用户名称
    showRegister: false, // 是否显示注册按钮
    radioCheckedRelId: null, // 选择的解绑目标relId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let teacherInfo = app.globalData.teacherInfo;
    let userType = app.globalData.userType;
    let showRegister = this.boolRegister(userType);
    let parentInfo = app.globalData.parentInfo;
    let isLogin;
    if (util.objIsNull(teacherInfo) && parentInfo.length <= 0) {
      isLogin = false;
    } else {
      isLogin = true;
    }
    this.setData({
      teacherInfo: teacherInfo,
      showRegister: showRegister,
      parentInfo: parentInfo,
      userType: userType,
      isLogin: isLogin
    })
  },
  /**
   * 去注册页面进行注册
   */
  toRegister: function () {
    let userType = this.data.userType;
    wx.navigateTo({
      url: '../register/register?type=' + userType,
    })
  },

  /**
   * 登录方法，暂时没用的方法
   */
  onLogin: function (e) {
    let that = this;
    wx.login({
      success: async res => {
        let loginRes = await requestUtil.request('POST', api.initUser, {
          code: res.code
        });
        if (loginRes.code == '0') {
          let openid = loginRes.data.data.openid;
          let teacherInfo = loginRes.data.data.teacherInfo;
          let parentInfo = loginRes.data.data.parentInfo;
          let userType = loginRes.data.data.userType;
          app.globalData.teacherInfo = teacherInfo;
          app.globalData.parentInfo = parentInfo;
          app.globalData.userType = userType;
          app.globalData.isInitTeacher = util.objIsNull(teacherInfo) ? false : true;
          app.globalData.isInitParent = parentInfo.length > 0 ? true : false;
          wx.setStorage({
            key: "openid",
            data: openid
          });
          wx.setStorage({
            key: "session_key",
            data: loginRes.data.data.session_key
          });
          that.setData({
            userType: userType,
            name: userType == null ? null : userType == 1 ? teacherInfo.name : parentInfo[0].name,
            isLogin: true,
            showRegister: that.boolRegister(userType)
          });
          //TODO 注册这里先不搞了
        } else {
          wx.showToast({
            title: '服务器出错，请联系管理员',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 切换角色
   */
  changeRole: function (e) {
    let userType = this.data.userType == null ? null : this.data.userType == 1 ? 2 : 1;
    let sign = userType == 1 ? '老师' : '家长';
    let showRegister = this.boolRegister(userType);
    this.setData({
      userType: userType,
      showRegister: showRegister
    })
    app.globalData.userType = userType;
    app.globalData.switchTab = true;
    wx.switchTab({
      url: '/pages/index/index/index',
      success: () => {
        wx.showToast({
          title: '切换为' + sign,
        })
      }
    })
  },

  /**
   * 是否显示注册按钮
   */
  boolRegister: function (userType) {
    if (userType == 1 && app.globalData.isInitTeacher) {
      return false;
    } else {
      return true;
    }
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      radioCheckedRelId: null,
      modalName: null
    })
  },

  submitModal(e) {
    let that = this;
    let relId = this.data.radioCheckedRelId;
    if (!this.data.parentInfo.length > 0) {
      wx.showToast({
        title: '暂无可解绑学生，请先绑定学生信息',
        icon: 'none'
      })
      return;
    }
    if (relId == null) {
      wx.showToast({
        title: '请先选择解绑的学生',
        icon: 'none'
      })
      return;
    }
    wx.request({
      url: api.unbindStudent + '?relId=' + relId,
      header: {
        'content-type': 'application/json',
        'xcx-token': getApp().xcxtoken
      },
      method: 'GET',
      success: res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        that.setData({
          radioCheckedRelId: null,
          modalName: null
        })
        that.getUserInfo();
      }
    })
  },

  radioChange(e) {
    this.setData({
      radioCheckedRelId: e.detail.value
    })
  },

  /**
   * 查询用户信息
   */
  async getUserInfo() {
    let openid = wx.getStorageSync('openid');
    let res = await requestUtil.request("POST", api.getUserInfo, {
      openid: openid
    });
    let userInfo = res.data.data;
    app.globalData.parentInfo = userInfo.parentInfo;
    app.globalData.teacherInfo = userInfo.teacherInfo;
    this.setData({
      parentInfo: userInfo.parentInfo,
      teacherInfo: userInfo.teacherInfo
    });
  }
})